"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库系统
Case Name   : 配置文件无权限，暂停数据库进程（接收kill -19信号
Create At   : 2020/07/27
@zou_jialiang0505328126
Description :
    1. 修改套接字文件权限为不可读不可写
    2. 获取pid
    3. 停止数据库进程
    4. 连接数据库
    5. 恢复数据库进程
    6. 连接数据库
    7. 运行tpcc指令
    8. 恢复配置文件权限
    9. 连接数据库
    10. 运行tpcc指令
Expect      :    
History     :   
    modified:2021/02/26 by @wan005:增加刷脏页时间
"""
import os
import unittest
import time
from yat.test import Node
from yat.test import macro
import sys

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.ComThread import ComThread

logger = Logger()
common = Common()
Constant = Constant()


class StopImmediate(unittest.TestCase):
    dbPrimaryUserNode = Node(node='PrimaryDbUser')
    dbPrimaryRootNode = Node(node='PrimaryRoot')
    DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
    DB_ENV_PATH = macro.DB_ENV_PATH
    GS_CTL_STOP_SUCCESS_MSG = Constant.GS_CTL_STOP_SUCCESS_MSG
    STANDBY_NORMAL = Constant.STANDBY_NORMAL
    PRIMARY_NORMAL = Constant.PRIMARY_NORMAL
    START_SUCCESS_MSG = Constant.START_SUCCESS_MSG
    TPCC_SUCCESS_MSG = Constant.TPCC_SUCCESS_MSG
    RESTART_SUCCESS_MSG = Constant.RESTART_SUCCESS_MSG
    file_path = ''
    TPCC_PATH = macro.TPCC_PATH
    primary_sh = CommonSH('PrimaryDbUser')
    standby_1_sh = CommonSH('Standby1DbUser')
    standby_2_sh = CommonSH('Standby2DbUser')
    conffile = macro.DB_INSTANCE_PATH + '/' + macro.DB_PG_CONFIG_NAME
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info("-----------this is setup-----------")
        logger.info('------------Opengauss_Reliability_Dbsys_Case170.py start------------')
        logger.info("----------------------get socket file path--------------")
        sql = "show unix_socket_directory"
        getfilepath_msg = self.primary_sh.executDbSql(sql)
        logger.info(getfilepath_msg)
        self.file_path = getfilepath_msg.split('\n')[2]
        logger.info(self.file_path)

    def test_stop_immediate(self):
        time.sleep(30)
        logger.info("------------------modify file permission----------------------")
        delete_cmd = f'chmod 000 {self.conffile}'
        logger.info(delete_cmd)

        logger.info("-----------------------stop pid-------------------------------")
        msg = common.kill_pid(self.dbPrimaryRootNode, '19')
        self.assertEqual(msg, '')

        logger.info("----------------------connect opengauss----------------------")
        nosocket_connect = ComThread(self.primary_sh.nosocketConnect, args=())
        nosocket_connect.setDaemon(True)
        nosocket_connect.start()

        socket_connect = ComThread(self.primary_sh.socket_connect, args=(self.file_path,))
        socket_connect.setDaemon(True)
        socket_connect.start()

        nosocket_connect.join(60 * 10)
        no_socket_connect_msg = nosocket_connect.get_result()
        logger.info('no_socket_connect_msg:' + str(no_socket_connect_msg))
        self.assertIn(Constant.TIMEOUT_EXPIRED_MSG, no_socket_connect_msg)

        socket_connect.join(60 * 10)
        socket_connect_msg = socket_connect.get_result()
        logger.info('socket_connect_msg:' + str(socket_connect_msg))
        self.assertIn(Constant.TIMEOUT_EXPIRED_MSG, no_socket_connect_msg)

        logger.info("-----------------------reconnect pid-------------------------------")
        msg = common.kill_pid(self.dbPrimaryRootNode, '18')
        self.assertEqual(msg, '')
        time.sleep(60)

        logger.info("----------------------connect opengauss----------------------")
        no_socket_connect_msg = self.primary_sh.nosocketConnect()
        logger.info(no_socket_connect_msg)
        self.assertEqual(no_socket_connect_msg, '')

        socket_connect_msg = self.primary_sh.socket_connect(self.file_path)
        logger.info(socket_connect_msg)
        self.assertEqual(socket_connect_msg, '')

        logger.info("---------------------start TPCC----------------------")
        tpcc_result = common.startTPCC(self.dbPrimaryRootNode, self.TPCC_PATH)
        logger.info('-------------' + tpcc_result + '-----------------------')
        logger.info(tpcc_result)
        self.assertTrue(tpcc_result.find(self.TPCC_SUCCESS_MSG) > -1)

        logger.info("------------------------recover socket file------------------")
        recover_cmd = f'chmod 700 {self.conffile}'
        logger.info(recover_cmd)
        self.dbPrimaryRootNode.sh(recover_cmd)

        logger.info("----------------------connect opengauss----------------------")
        no_socket_connect_msg = self.primary_sh.nosocketConnect()
        logger.info(no_socket_connect_msg)
        self.assertEqual(no_socket_connect_msg, '')

        socket_connect_msg = self.primary_sh.socket_connect(self.file_path)
        logger.info(socket_connect_msg)
        logger.info(type(socket_connect_msg))
        self.assertEqual(socket_connect_msg, '')

        logger.info("---------------------start TPCC----------------------")
        tpcc_result = common.startTPCC(self.dbPrimaryRootNode, self.TPCC_PATH)
        logger.info('-------------' + tpcc_result + '-----------------------')
        logger.info(tpcc_result)
        self.assertTrue(tpcc_result.find(self.TPCC_SUCCESS_MSG) > -1)

        # 校验刷脏页是否完成
        logger.info('校验刷脏页是否完成')
        dirty_page_finshed_flag = self.primary_sh.check_whether_remain_dirty_page()
        if not dirty_page_finshed_flag:
            logger.info('===脏页最长等待时间为1h=')
            dirty_page_finshed_flag = \
                self.primary_sh.check_whether_remain_dirty_page()
        self.assertTrue(dirty_page_finshed_flag)

        # 校验备机是否完成数据同步
        logger.info('校验备机是否完成数据同步')
        flag_1 = self.standby_1_sh.check_data_consistency()
        self.assertTrue(flag_1)
        flag_2 = self.standby_2_sh.check_data_consistency()
        self.assertTrue(flag_2)

    def tearDown(self):
        logger.info('----------------this is tearDown-----------------------')
        msg = common.kill_pid(self.dbPrimaryRootNode, '18')
        logger.info(msg)

        logger.info("------------------------recover socket file------------------")
        recover_cmd = f'chmod 700 {self.conffile}'
        logger.info(recover_cmd)
        self.dbPrimaryRootNode.sh(recover_cmd)
